/******************
变更人员
* */
<template>
  <el-dialog
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="50%"
    top="20vh"
    @close="closeDialog()"
  >
    <el-form
      ref="dataForm"
      :rules="formRules"
      :model="dataForm"
      :inline="true"
      label-position="right"
      style="margin: 0 10px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-row style="display: flex;justify-content: space-between;border: 1px solid  rgb(176, 189, 226);position: relative;height: 100px;padding-top: 20px;border-radius: 5px">
          <span  style="position: absolute;top: -10px;left: 50px;font-size: 16px;background: #FFFFFF;padding: 0 10px;color:#0e258c">原员工信息</span>
          <el-form-item label="员工编号">
            <span>{{dataForm.assessUserName}}</span>
          </el-form-item>

          <el-form-item label="姓名">
            <span>{{dataForm.assessRealName}}</span>
          </el-form-item>

          <el-form-item label="部门">
            <span>{{dataForm.assessDeptName}}</span>
          </el-form-item>
      </el-row>
      <el-row style="display: flex;justify-content: space-between;border: 1px solid  rgb(176, 189, 226);position: relative;height: 100px;padding-top: 20px;border-radius: 5px;margin-top: 25px">
        <span  style="position: absolute;top: -10px;left: 50px;font-size: 16px;background: #FFFFFF;padding: 0 10px;color:#0e258c">
              <el-tooltip effect="dark" content="提示：调入人员将接管原员工的全部考核指标信息" placement="top-start">
                <span>调入人员信息<i class="el-icon-question" style="margin-left:10px;"></i></span>
              </el-tooltip>
        </span>
        <el-col  :span="19">
          <el-form-item label="" prop="ids" style="width: 100%" >
            <el-button icon="el-icon-user-solid" @click="showPerson" type="text" ></el-button>
            <span v-for="it in options" :key="it.assessUserId">
            <!--  <el-tooltip
                class="item"
                :disabled="options.length < 3"
                effect="dark"
                :content="shaiXuan(options, '被考核人')"
                placement="top-start"
              >-->
                <span style="font-size: 16px; color: #303745">{{ it.assessRealName }}</span>
              <!--</el-tooltip>-->
            </span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" plain @click="handleConfirm">确定</el-button>
      <el-button type="primary" plain @click="closeDialog()">返回</el-button>
    </span>
    <UserCommonSelect v-if="usercomment" ref="usercomment" @submitfy="submitfy"></UserCommonSelect>

  </el-dialog>
</template>

<script>
import UserCommonSelect from "@/views/dailyExamine/components/usercomment-selectCopy-zb"; //人员选择的组件
import { changePerson} from "@/api/dailyExamine/targetExamine/perORtarChange";
import {zltest} from "@/api/dailyExamine/targetExamine/createAssessment";
export default {
  data() {
      var validateIDs = (rule, value, callback) => {
          if (this.options.length === 0) {
              callback(new Error("请选择被考核人"));
          } else {
              zltest(
                  Object.assign(
                      {
                          assessId: this.fatherData.id, //考核周期id
                          type: 0, //考核对象类型，0人员，1机构
                          participantIds: this.getUserIds(this.options) //this.getUserIds(this.options)// 637//536//考核对象(人员或机构id) this.getUserIds(this.options)
                      },
                      ""
                  )
              )
                  .then(r => {
                      if (r.msg) {
                          callback(new Error("同一周期内被考核人不可以重复"));
                      } else {
                          callback();
                      }
                  })
                  .catch(err => {});
          }
      };

    return {
      canDelete: true, //是否能删除指标
      chooseTarget: [], //选中的指标
      repeatTargetList: [], //复用指标数组
      repeatList: [], //被选中的复用指标
      repeatVisible: false, //复用指标弹窗
      fatherData: {}, //新增时总部启动的考核数据
      dialogVisible: false, //考核指标库显隐
      multipleSelection: [], //选中的指标
      dataList: [], //指标数组
      usercomment: false, //人员组件显隐
      options: [], //被考核人的数组
      dialogTitle: "新增人员考核", //弹窗标题
      visible: false, //最外层弹窗显隐
      dataForm: {
        //数据表单
        ids: []
      },
      formRules: {
        //校验规则
          ids: [{ validator:validateIDs, trigger: "change" }]
      },
      submitForm: {
        //要提交给后台的数据对象
        name: null, //考核名称
        participantList: [], //被考核人
        kpiDetailList: [], //指标
        type: 0 //类型 0是人员 1 是机构
      },
      chooseDeptData: {}, //设置数据提供人的时候当前数据对象
      //news 2022/4/8
      templateNamesList: [],
      userArr:[]
    };
  },
  components: {
    UserCommonSelect,
   // zhibiaoTable
  },
  watch: {
    // 监听指标选中变化
    multipleSelection: {
      handler(val) {
        if (val.length !== 0) {
          this.canDelete = false;
        } else {
          this.canDelete = true;
        }
      },
      deep: true
    }
  },
  methods: {
    selecteRefresh(){
        this.$forceUpdate()
    },
    //筛选出姓名与职务
    shaiXuan(data, type) {
      var list = [],
        obj = {};
      if (type == "被考核人") {
        for (var i in data) {
          list.push(data[i].userName);
        }
      }
      return list.toString();
    },
    // 展示数据提供部门
    showDataProvide(data) {
      this.usercomment = true;
      this.$nextTick(() => {
        this.$refs.usercomment.init(1, "shr", JSON.stringify([]), "isFather");
      });
      this.chooseDeptData = data;
    },
    // 子组件穿过来的数据
    getRow(rows) {
      this.chooseTarget = rows;
    },
    // 被考核人改变
    personSelectChange(data) {

      this.participantList = [];
        this.options.map(it => {
          this.participantList.push({
            assessDeptId: it.deptId,
            assessDeptName: it.deptName,
            assessRealName: it.userName,
            assessUserId: it.userId,
            assessUserName: it.userCode
          });
        });
        this.options = this.participantList.slice(0,1) ;
    },
    //   人员展示
    showPerson() {
      this.usercomment = true;
      this.$nextTick(() => {
        // this.$refs.usercomment.init({}, "cyr", JSON.stringify({}));
        this.$refs.usercomment.init(
          "12",
          "sy",
          JSON.stringify( this.userArr),
          "htcj",
          "新增",
          undefined
        );
        this.userArr = [] ;
      });
      this.$refs["dataForm"].clearValidate();
    },
    // 选择人后的提交
    submitfy(data) {
      if (data.type == "shr") {
        this.dataList.map(it => {
          if (it.normId == this.chooseDeptData.normId) {
            this.$set(it, "provideRealName", data.tableDataUser[0].userName);
            this.$set(it, "provideUserName", data.tableDataUser[0].userCode);
            this.$set(it, "provideUserId", data.tableDataUser[0].userId);
            this.$set(it, "provideDeptName", data.tableDataUser[0].deptName);
            this.$set(it, "provideDeptId", data.tableDataUser[0].deptId);
          }
        });
      } else {
        this.options = data.tableDataUser;
        if (this.options.length>1){
            this.$message.error("只能选择一个人")
        }
        this.userArr.push(this.options[0] );
        this.personSelectChange();
      }
    },
    // 确定新增
    handleConfirm() {
     let params = this.options[0] ;
     console.log(params)
      this.$refs.dataForm.validate(valid => {
        if (valid) {
            params.id = this.dataForm.id;
            changePerson(params)
            .then(r => {
              if (r.data.code == 0) {
                this.$message.success("变更成功");
                this.$emit("closeDialog");
                this.$emit("refesData");
              }
            })
            .catch(err => {
              this.$message.error(r.data.msg);
              this.$emit("closeDialog");
              this.$emit("refesData");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 初始化
    init(data, editData) {
      this.visible = true;
      this.fatherData = data.data;
      if (editData) {
        if (editData) {
          this.dialogTitle = "变更考核人";
          this.dataForm = editData;
        }
      }
    },
    // 表单提交
    dataFormSubmit() {},
    // 关闭
    closeDialog() {
      this.visible = false;
      this.$emit("closeDialog");
    },
    getUserIds(userArr) {
        let ids = [];
        userArr.map(it => {
            ids.push(it.assessUserId);
        });
        return ids.toString();
    },
  }
};
</script>

<style lang="scss">
</style>
